import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'

export type PageType = 'router' | 'dropdown' | 'hash' | '_blank'
export interface PageItem {
  path?: string
  type?: PageType
  params?: string
  label: string
  options?: PageItem[]
}
export function useNavigation() {
  const router = useRouter()
  const { t } = useI18n()
  const openPage = (pageItem: PageItem) => {
    const { path, type, params } = pageItem
    if (!path)
      return
    if (type === 'router') {
      const routerPath = params === undefined ? path : `${path}/${t(params)}`
      router.push(routerPath)
    }
    if (type === 'hash') {
      router.push(`${path}-${Date.now()}`)
    }
    if (type === '_blank') {
      window.open(location.origin + path, '_blank')
    }
  }

  const openRemotePage = (url?: string) => {
    if (!url)
      return
    if (url.startsWith('http')) {
      return window.open(url, '_blank')
    }
    if (url.startsWith('web_blank')) {
      return window.open(url.replace('web_blank:/', ''), '_blank')
    }
    if (url.startsWith('web')) {
      return router.push(url.replace('web://', ''))
    }
  }

  return {
    openPage,
    openRemotePage,
  }
}
